﻿// Decompiled with JetBrains decompiler
// Type: CyberWin.Work.Trade.Net.Ynwlzc.QrBatch.Form1
// Assembly: CyberWin.Work.Trade.Net.Ynwlzc.QrBatch, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 2F728526-1731-4244-8B7C-00DFB52E344F
// Assembly location: G:\未来之窗源码备份\visaulstudio\未来之窗二维码批量识别\二维码批量识别-未来之窗.exe

using CyberWin.CSHARP.YNWLZC.WebFrame.CyberPHP.CyberWinOutput.DataExport;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ZXing;
using ZXing.Common;

namespace CyberWin.Work.Trade.Net.Ynwlzc.QrBatch
{
  public class Form1 : Form
  {
    private IContainer components;
    private Button btn_打开图片;
    private PictureBox barCodeImg;
    private Button btn_读取二维码;
    private TextBox ContentTxt;
    private FolderBrowserDialog folderBrowserDialog1;
    private DataGridView dgv_fileqr;
    private Button btn_浏览文件夹;
    private TextBox txt_folder;
    private Button btn_savebatch;
    private Button btn_batch_解码;
    private Button btn_列举文件;
    private DataGridViewTextBoxColumn FileName;
    private DataGridViewTextBoxColumn QR;
    private DataGridViewTextBoxColumn status;
    private DataGridViewTextBoxColumn FileFullPath;
    private Button button1;
    private Button button_读取二维码TH;

    protected override void Dispose(bool disposing)
    {
      if (disposing && this.components != null)
        this.components.Dispose();
      base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
      this.btn_打开图片 = new Button();
      this.barCodeImg = new PictureBox();
      this.btn_读取二维码 = new Button();
      this.ContentTxt = new TextBox();
      this.folderBrowserDialog1 = new FolderBrowserDialog();
      this.dgv_fileqr = new DataGridView();
      this.FileName = new DataGridViewTextBoxColumn();
      this.QR = new DataGridViewTextBoxColumn();
      this.status = new DataGridViewTextBoxColumn();
      this.FileFullPath = new DataGridViewTextBoxColumn();
      this.btn_浏览文件夹 = new Button();
      this.txt_folder = new TextBox();
      this.btn_savebatch = new Button();
      this.btn_batch_解码 = new Button();
      this.btn_列举文件 = new Button();
      this.button1 = new Button();
      this.button_读取二维码TH = new Button();
      ((ISupportInitialize) this.barCodeImg).BeginInit();
      ((ISupportInitialize) this.dgv_fileqr).BeginInit();
      this.SuspendLayout();
      this.btn_打开图片.Location = new Point(869, 32);
      this.btn_打开图片.Name = "btn_打开图片";
      this.btn_打开图片.Size = new Size(87, 32);
      this.btn_打开图片.TabIndex = 0;
      this.btn_打开图片.Text = "打开图片";
      this.btn_打开图片.UseVisualStyleBackColor = true;
      this.btn_打开图片.Click += new EventHandler(this.btn_打开图片_Click);
      this.barCodeImg.Location = new Point(511, 56);
      this.barCodeImg.Name = "barCodeImg";
      this.barCodeImg.Size = new Size(302, 273);
      this.barCodeImg.SizeMode = PictureBoxSizeMode.Zoom;
      this.barCodeImg.TabIndex = 1;
      this.barCodeImg.TabStop = false;
      this.btn_读取二维码.Location = new Point(869, 70);
      this.btn_读取二维码.Name = "btn_读取二维码";
      this.btn_读取二维码.Size = new Size(87, 30);
      this.btn_读取二维码.TabIndex = 2;
      this.btn_读取二维码.Text = "读取二维码";
      this.btn_读取二维码.UseVisualStyleBackColor = true;
      this.btn_读取二维码.Click += new EventHandler(this.btn_读取二维码_Click);
      this.ContentTxt.Location = new Point(12, 349);
      this.ContentTxt.Name = "ContentTxt";
      this.ContentTxt.Size = new Size(536, 21);
      this.ContentTxt.TabIndex = 3;
      this.dgv_fileqr.AllowUserToAddRows = false;
      this.dgv_fileqr.AllowUserToDeleteRows = false;
      this.dgv_fileqr.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
      this.dgv_fileqr.Columns.AddRange((DataGridViewColumn) this.FileName, (DataGridViewColumn) this.QR, (DataGridViewColumn) this.status, (DataGridViewColumn) this.FileFullPath);
      this.dgv_fileqr.Location = new Point(12, 32);
      this.dgv_fileqr.Name = "dgv_fileqr";
      this.dgv_fileqr.ReadOnly = true;
      this.dgv_fileqr.RowTemplate.Height = 23;
      this.dgv_fileqr.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      this.dgv_fileqr.Size = new Size(493, 311);
      this.dgv_fileqr.TabIndex = 4;
      this.FileName.HeaderText = "文件名";
      this.FileName.Name = "FileName";
      this.FileName.ReadOnly = true;
      this.QR.HeaderText = "二维码";
      this.QR.Name = "QR";
      this.QR.ReadOnly = true;
      this.status.HeaderText = "状态";
      this.status.Name = "status";
      this.status.ReadOnly = true;
      this.FileFullPath.HeaderText = "文件路径";
      this.FileFullPath.Name = "FileFullPath";
      this.FileFullPath.ReadOnly = true;
      this.btn_浏览文件夹.Location = new Point(403, 3);
      this.btn_浏览文件夹.Name = "btn_浏览文件夹";
      this.btn_浏览文件夹.Size = new Size(75, 23);
      this.btn_浏览文件夹.TabIndex = 5;
      this.btn_浏览文件夹.Text = "浏览文件夹";
      this.btn_浏览文件夹.UseVisualStyleBackColor = true;
      this.btn_浏览文件夹.Click += new EventHandler(this.btn_浏览文件夹_Click);
      this.txt_folder.Location = new Point(12, 3);
      this.txt_folder.Name = "txt_folder";
      this.txt_folder.Size = new Size(379, 21);
      this.txt_folder.TabIndex = 6;
      this.btn_savebatch.Location = new Point(690, 3);
      this.btn_savebatch.Name = "btn_savebatch";
      this.btn_savebatch.Size = new Size(114, 21);
      this.btn_savebatch.TabIndex = 7;
      this.btn_savebatch.Text = "导出结果";
      this.btn_savebatch.UseVisualStyleBackColor = true;
      this.btn_savebatch.Click += new EventHandler(this.btn_savebatch_Click);
      this.btn_batch_解码.Location = new Point(565, 3);
      this.btn_batch_解码.Name = "btn_batch_解码";
      this.btn_batch_解码.Size = new Size(119, 21);
      this.btn_batch_解码.TabIndex = 8;
      this.btn_batch_解码.Text = "批量读取二维码";
      this.btn_batch_解码.UseVisualStyleBackColor = true;
      this.btn_batch_解码.Click += new EventHandler(this.btn_batch_解码_Click);
      this.btn_列举文件.Location = new Point(484, 3);
      this.btn_列举文件.Name = "btn_列举文件";
      this.btn_列举文件.Size = new Size(75, 23);
      this.btn_列举文件.TabIndex = 9;
      this.btn_列举文件.Text = "列举文件";
      this.btn_列举文件.UseVisualStyleBackColor = true;
      this.btn_列举文件.Click += new EventHandler(this.btn_列举文件_Click);
      this.button1.Location = new Point(869, 106);
      this.button1.Name = "button1";
      this.button1.Size = new Size(87, 30);
      this.button1.TabIndex = 10;
      this.button1.Text = "裁剪";
      this.button1.UseVisualStyleBackColor = true;
      this.button1.Click += new EventHandler(this.button1_Click);
      this.button_读取二维码TH.Location = new Point(869, 151);
      this.button_读取二维码TH.Name = "button_读取二维码TH";
      this.button_读取二维码TH.Size = new Size(87, 30);
      this.button_读取二维码TH.TabIndex = 11;
      this.button_读取二维码TH.Text = "读取二维码TH";
      this.button_读取二维码TH.UseVisualStyleBackColor = true;
      this.button_读取二维码TH.Click += new EventHandler(this.button_读取二维码TH_Click);
      this.AutoScaleDimensions = new SizeF(6f, 12f);
      this.AutoScaleMode = AutoScaleMode.Font;
      this.ClientSize = new Size(1000, 377);
      this.Controls.Add((Control) this.button_读取二维码TH);
      this.Controls.Add((Control) this.button1);
      this.Controls.Add((Control) this.btn_列举文件);
      this.Controls.Add((Control) this.btn_batch_解码);
      this.Controls.Add((Control) this.btn_savebatch);
      this.Controls.Add((Control) this.txt_folder);
      this.Controls.Add((Control) this.btn_浏览文件夹);
      this.Controls.Add((Control) this.dgv_fileqr);
      this.Controls.Add((Control) this.ContentTxt);
      this.Controls.Add((Control) this.btn_读取二维码);
      this.Controls.Add((Control) this.barCodeImg);
      this.Controls.Add((Control) this.btn_打开图片);
      this.Name = nameof (Form1);
      this.Text = "未来之窗二维码批量识别工具";
      ((ISupportInitialize) this.barCodeImg).EndInit();
      ((ISupportInitialize) this.dgv_fileqr).EndInit();
      this.ResumeLayout(false);
      this.PerformLayout();
    }

    public Form1() => this.InitializeComponent();

    private void btn_打开图片_Click(object sender, EventArgs e)
    {
      OpenFileDialog openFileDialog = new OpenFileDialog();
      openFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
      openFileDialog.Filter = "图形文件(*.jpg)|*.jpg";
      if (openFileDialog.ShowDialog() != DialogResult.OK)
        return;
      this.barCodeImg.Image = Image.FromFile(openFileDialog.FileName);
    }

    private void btn_读取二维码_Click(object sender, EventArgs e)
    {
      DecodingOptions decodingOptions = new DecodingOptions();
      decodingOptions.PossibleFormats = (IList<BarcodeFormat>) new List<BarcodeFormat>()
      {
        BarcodeFormat.QR_CODE
      };
      BarcodeReader barcodeReader = new BarcodeReader();
      barcodeReader.Options = decodingOptions;
      Result result = barcodeReader.Decode(this.barCodeImg.Image as Bitmap);
      if (result == null)
      {
        this.ContentTxt.Text = "读取失败";
        int num = (int) MessageBox.Show("读取失败");
      }
      else
      {
        this.ContentTxt.Text = result.Text;
        int num = (int) MessageBox.Show("读取成功，内容：" + result.Text);
      }
    }

    private void btn_浏览文件夹_Click(object sender, EventArgs e)
    {
      if (this.folderBrowserDialog1.ShowDialog() != DialogResult.OK)
        return;
      this.txt_folder.Text = this.folderBrowserDialog1.SelectedPath;
    }

    private void ListPicFromFolder(string path)
    {
      foreach (FileSystemInfo fileSystemInfo in new DirectoryInfo(path).GetFileSystemInfos())
      {
        if (fileSystemInfo is FileInfo)
        {
          FileInfo fileInfo = (FileInfo) fileSystemInfo;
          Console.WriteLine(Path.GetDirectoryName(fileInfo.FullName));
          string extension = Path.GetExtension(fileInfo.FullName);
          string withoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
          if (extension == ".jpg")
            this.addFiletodata(withoutExtension, fileInfo.FullName);
        }
      }
    }

    private void addFiletodata(string filname, string FileFullPath) => this.dgv_fileqr.Rows.Add((object) filname, (object) "", (object) "0", (object) FileFullPath);

    private void btn_列举文件_Click(object sender, EventArgs e) => this.ListPicFromFolder(this.txt_folder.Text);

    private void btn_savebatch_Click(object sender, EventArgs e)
    {
      SaveFileDialog saveFileDialog = new SaveFileDialog();
      saveFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
      saveFileDialog.Filter = "未来之窗 Excel 文件(*.xls)|*.xls";
      if (saveFileDialog.ShowDialog() != DialogResult.OK)
        return;
      CL_HTML.导出_DataGridView_ToHtmlExcel(this.dgv_fileqr, saveFileDialog.FileName, "未来之窗二维码导出");
    }

    private void btn_batch_解码_Click(object sender, EventArgs e)
    {
      if (this.dgv_fileqr.Rows.Count < 1)
        return;
      string str = "1";
      foreach (DataGridViewRow row in (IEnumerable) this.dgv_fileqr.Rows)
      {
        string filename = row.Cells["FileFullPath"].Value.ToString();
        if (filename.Length > 5)
        {
          string qr = this._parseQr(filename);
          row.Cells["QR"].Value = (object) qr;
        }
        row.Cells["status"].Value = (object) str;
      }
    }

    public string _parseQr(string filename)
    {
      this.barCodeImg.Image = Image.FromFile(filename);
      DecodingOptions decodingOptions = new DecodingOptions();
      decodingOptions.PossibleFormats = (IList<BarcodeFormat>) new List<BarcodeFormat>()
      {
        BarcodeFormat.QR_CODE
      };
      BarcodeReader barcodeReader = new BarcodeReader();
      barcodeReader.Options = decodingOptions;
      Result result = barcodeReader.Decode(this.barCodeImg.Image as Bitmap);
      string str;
      if (result == null)
      {
        this.ContentTxt.Text = "读取失败";
        str = "读取失败";
      }
      else
      {
        this.ContentTxt.Text = result.Text;
        str = result.Text;
      }
      return str;
    }

    private void button1_Click(object sender, EventArgs e) => this.barCodeImg.Image = (Image) Form1.Cut(new Bitmap(this.barCodeImg.Image), 272, 692, 570, 570);

    public static Bitmap Cut(Bitmap b)
    {
      if (b == null)
        return (Bitmap) null;
      int x = b.Width * 3 / 4;
      int y = 0;
      int width = b.Width / 4;
      int height = b.Height / 4;
      try
      {
        Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        Graphics graphics = Graphics.FromImage((Image) bitmap);
        graphics.DrawImage((Image) b, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
        graphics.Dispose();
        return bitmap;
      }
      catch
      {
        return (Bitmap) null;
      }
    }

    public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
    {
      if (b == null)
        return (Bitmap) null;
      int width = b.Width;
      int height = b.Height;
      if (StartX >= width || StartY >= height)
        return (Bitmap) null;
      if (StartX + iWidth > width)
        iWidth = width - StartX;
      if (StartY + iHeight > height)
        iHeight = height - StartY;
      try
      {
        Bitmap bitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
        Graphics graphics = Graphics.FromImage((Image) bitmap);
        graphics.DrawImage((Image) b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
        graphics.Dispose();
        return bitmap;
      }
      catch
      {
        return (Bitmap) null;
      }
    }

    private void button_读取二维码TH_Click(object sender, EventArgs e) => this.ContentTxt.Text = new QRCodeDecoder().decode((QRCodeImage) new QRCodeBitmapImage(new Bitmap(this.barCodeImg.Image)));
  }
}
